/**
 * 代码生成器
 * @type {{screenInit}} 筛选
 */
var formGenerator = function () {

    /**
     * 查询表单
     * @param elem   divId
     * @param inputs [{name:"userName",text="用户名",type:"text,time"}]
     * @param gridOpt grid表格
     */
    var screenInit = function (obj) {
        var elem = obj.elem;
        var inputs = obj.inputs;
        var gridOpt = obj.gridOpt;

        function getInputDiv(i, item) {
            var type = item.type === undefined ? "text" : item.type;
            if (type === "hidden") {
                return getInputHtml(formId, item);
            }
            var input = getInputHtml(formId, item);
            if (item.name2) {
                input +="<span style='float: left'>-</span>";
                item.placeholder = item.placeholder2 ? item.placeholder2 : "";
                input += getInputHtml(formId, item, item.name2);
            }
            return '<div class="block">' +
                '       <div class="label">' + item.text + '： </div>' +
                '       <div class="input1 input">' +
                '       ' + input +
                '       </div>' +
                '   </div>';
        }

        function addOnInputFocus(formId, opt) {
            $(formId).find("input[type=text]").focus(function () {
                var inputText = $(this);
                setTimeout(function () {
                    inputText.after("<input type='button' id='" + opt.id + "_sonPage_btn' class='input_search_btn'/>")
                    $("#" + opt.id + "_sonPage_btn").bind("click", function () {
                        opt.param = $(formId).serialize();
                        $("#" + opt.id).wdfGrid("reload", opt);
                    });

                }, 200);
            });
            $(formId).find("input[type=text]").blur(function () {
                var inputText = $(this);
                setTimeout(function () {
                    inputText.next(".input_search_btn").remove();
                }, 200);

            })
        }

        function addSelectOnChang(formId, inputs, opt) {
            $.each(inputs, function (i, item) {
                if (item.type === 'select' || item.type === 'zTree') {
                    $(formId).find("input[name=" + item.name + "]").change(function () {
                        opt.param = $(formId).serialize();
                        $("#" + opt.id).wdfGrid("reload", opt);
                    });
                }
            });
        }

        function addDivKeyDownClick(elem, formId, opt) {
            $(elem).unbind("keydown");
            $(elem).bind("keydown", function (e) {
                // 兼容FF和IE和Opera
                var theEvent = e || window.event;
                var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
                if (code === 13) {
                    //回车执行查询
                    opt.param = $(formId).serialize();
                    $("#" + opt.id).wdfGrid("reload", opt);
                }
            });
        }

        function resetSearchClick(formId, opt) {
            $(elem + " .reset_btn").click(function () {
                opt.param = "";
                $("#" + formId)[0].reset();
                $("#" + formId).find("input").not(".input_button").val("");
                $("#" + opt.id).wdfGrid("reload", opt);
            });
            $(elem + " .search_btn").click(function () {
                opt.param = $("#" + formId).serialize();
                $("#" + opt.id).wdfGrid("reload", opt);
            })
        }

        var inputHtml = "";
        var laydateIds = [];
        var formId = $(elem).attr('id') + "Form";

        $.each(inputs, function (i, item) {
            if (isTimeType(item)) {
                laydateIds.push({id: formId + '_' + item.name, type: item.type});
                if (item.name2)
                    laydateIds.push({id: formId + '_' + item.name2, type: item.type});
                item.type = "text";
                inputHtml += getInputDiv(i, item);
            } else {
                inputHtml += getInputDiv(i, item);
            }
        });
        var html =
            '<form id="' + formId + '">' +
            '   <div class="searchBar_box">';
        if (!obj.haveNoAddBtn) {
            html += '       <input type="button" class="input_button addnewproject" onclick="add()" value="新增">';
        }

        html += '       <input type="button" class="input_button tosearch_panel"  value="点击查询"/><input type="button" class="input_button hideSearch_panel"  value="点击查询"/>' +
            '       <div class="searchBar_Blue" id="searchPanel">' +
            '           <div class="searchInput_left">' +
            '                ' + inputHtml +
            '           </div>' +
            '                <div class="optContent">' +
            '                    <input type="button" class="input_button search_btn" value="查询" />' +
            '                    <input type="button" id="resetSearchBut"  class="input_button reset_btn" value="重置"/>' +
            '               </div>' +
            '       <div style="clear:both"></div>' +
            '       </div>' +
            '       <div style="clear:both"></div>' +
            '   </div>' +
            '</form>';
        $(elem).html(html);
        $(".tosearch_panel").click(function (e) {
            $(this).parents(".searchBar_box").find(".searchBar_Blue").slideDown(100);
            stopPropagation(e);
            $(this).hide();
            $(this).next(".hideSearch_panel").show();
        });
        $(".searchBar_Blue").click(function (e) {
            stopPropagation(e);
        });
        if (obj.add !== undefined) {
            if (obj.add.hidden) {
                $(elem + " .addnewproject").hide()
            }
            if (obj.add.value) {
                $(elem + " .addnewproject").val(obj.add.value)
            }
            if (obj.add.name) {
                $(elem + " .addnewproject").val(obj.add.name)
            }
        }
        $.ajaxSettings.async = false;
        setSelectData(elem, inputs, true);
        addDivKeyDownClick(elem, "#" + formId, gridOpt);
        //   addOnInputFocus("#" + formId, gridOpt);
        addSelectOnChang("#" + formId, inputs, gridOpt);
        resetSearchClick(formId, gridOpt);
        initLayDate(laydateIds, function () {
            setTimeout(function () {

                gridOpt.param = $("#" + formId).serialize();
                $("#" + gridOpt.id).wdfGrid("reload", gridOpt);
            }, 50);
        });
        $.ajaxSettings.async = true;
    };

    /**
     * 新增界面表单生成 bootstrap
     * @param divId 放到哪个 id
     * @param columns columns [{name:"userName",text="用户名",type:"text,time"}]
     */
    var addFormInit = function (obj) {
        var elem = obj.elem;
        var inputs = obj.inputs;
        var inputs2 = obj.inputs2;
        var laydateIds = [];

        function getInputDiv(i, input) {
            let requiredHtml = "";
            if (input.required !== undefined) {
                requiredHtml = '<lable class="required_lable">*</lable>';
            }
            var type = input.type === undefined ? "text" : input.type;
            if (type === "hidden") {
                return '<input type="' + type + '" id="' + formId + '_' + input.name + '" name="' + input.name + '" ' + getInputParam(input) + ' />';
            }
            return '<div class="form-group">' +
                '        <label class="col-md-2 col-sm-3 control-label">' + input.text + '</label>' +
                '        <div class="col-md-10 col-sm-9">' +
                '            <div class="col-sm-12"><input type="' + type + '" class="form-control" id="' + formId + '_' + input.name + '" name="' + input.name + '" placeholder="请输入' + input.text + '" ' + getInputParam(input) + ' >' + requiredHtml + '</div>' +
                '            <div class="col-sm-10 error_msg" ></div>' +
                '        </div>' +
                '    </div>';
        }

        function getTextareaInput(i, input) {
            let requiredHtml = "";
            if (input.required !== undefined) {
                requiredHtml = '<lable class="required_lable">*</lable>';
            }
            return '<div class="form-group">' +
                '        <label class="col-md-2 col-sm-3 control-label">' + input.text + '</label>' +
                '        <div class="col-md-10 col-sm-9">' +
                '            <div class="col-sm-12"><textarea type="text" class="form-control" id="' + formId + '_' + input.name + '" name="' + input.name + '" placeholder="请输入' + input.text + '" rows="3" ' + getInputParam(input) + ' ></textarea>' + requiredHtml + '</div>' +
                '            <div class="col-sm-10 error_msg" ></div>' +
                '        </div>' +
                '    </div>';
        }

        function getSelectInput(i, item) {
            let requiredHtml = "";
            if (item.required !== undefined) {
                requiredHtml = '<lable class="required_lable">*</lable>';
            }
            var optionsHtml = "";
            if (item.options !== undefined)
                $.each(item.options, function (i, item) {
                    //optionsHtml += '<option value=' + item.value + '>' + item.text + '</option>';
                    optionsHtml += '<option value=' + item.value + '  data_orgcoding='+ item.value +' >' + item.text + '</option>';
                });
            return '<div class="form-group">' +
                '    <label class="col-md-2 col-sm-3 control-label">' + item.text + '</label>' +
                '    <div class="col-md-10 col-sm-9">' +
                '       <div class="col-sm-12">' +
                '           <select name="' + item.name + '" class="form-control selectpicker show-tick form-control" data-live-search="true"' + getInputParam(item) + '>' +
                '           ' + optionsHtml + '' +
                '           </select>' +
                '           ' + requiredHtml +
                '       </div>' +
                '       <div class="col-sm-10 error_msg" ></div>' +
                '    </div>' +
                '</div>';
        }

        function setSelectData(elem, inputs) {
            $.each(inputs, function (i, item) {
                if (item.type === "select" && item.param !== undefined) {
                    var forName = item.param.for;
                    if (forName !== undefined) {
                        $(elem + " select[name='" + forName + "']").change(function (ev, changeCall) {
                            requestSelectData(elem, item, changeCall);
                        })
                    } else {
                        requestSelectData(elem, item)
                    }
                }
            });
        }

        function requestSelectData(elem, inputsItem, changeCall) {
            var param = inputsItem.param;
            var url = param.url;
            var forId = param.for;
            if (forId !== undefined) {
                url += $(elem + " select[name='" + forId + "']").val();
            }
            $.get(url, function (obj) {
                if (obj.length === 0) {
                    return;
                }
                var html = '<option value="">请选择</option>';
                var data = eval("obj." + param.data);
                $.each(data, function (i, item) {
                    var value = eval("item." + param.value);
                    var text = eval("item." + param.text);
                    html += '<option value=' + value + '>' + text + '</option>'
                });
                $(elem + " select[name='" + inputsItem.name + "']").html(html);
                if (changeCall !== undefined)
                    changeCall.call(this, data)
                $(elem + " select[name='" + inputsItem.name + "']").change(function () {
                    var val = $(this).val();
                    $.each(data, function (i, item) {
                        if (val == eval("item." + param.value)) {
                            $(elem + " select[name='" + inputsItem.name + "']").trigger("select", [item, item]);
                            return false;
                        }
                    });

                })
            });
        }

        function getHtml(i, item) {
            var html = "";
            if (isTimeType(item)) {
                laydateIds.push({id: formId + '_' + item.name, type: item.type});
                item.type = "text";
                html += getInputDiv(i, item);
            } else if (item.type === "select") {
                html += getSelectInput(i, item);
            } else if (item.type === "textarea") {
                html += getTextareaInput(i, item);
            } else {
                html += getInputDiv(i, item);
            }
            return html;
        }

        var html;
        var html_left = "";
        var html_right = "";
        var formId = $(elem).attr('id') + "Form";
        $.each(inputs, function (i, item) {
            html_left += getHtml(i, item);
        });
        html = html_left;
        if (inputs2 !== undefined) {
            $.each(inputs2, function (i, item) {
                html_right += getHtml(i, item);
            });
            html = '<div class="col-sm-6">' + html_left + '</div><div class="col-sm-6">' + html_right + '</div>';
        }
        var formHtml =
            '<form id="' + formId + '" class="form-horizontal col-md-10 col-md-offset-1 col-xs-10 col-xs-offset-1" role="form">' +
            '   <div class="form-group" style="margin-top: 60px">' +
            '       ' + html +
            '       <div class="col-xs-12 text-center">' +
            '           <button type="button" class="button_ok" >保存</button>' +
            '           <button type="button" class="button_close">关闭</button>' +
            '       </div>' +
            '       </div>' +
            '</form>';
        $.ajaxSettings.async = false;
        $(elem).empty();
        $(elem).append(formHtml);
        initLayDate(laydateIds);
        setSelectData(elem, inputs);
        $.ajaxSettings.async = true;
        $(elem + " .button_ok").click(function () {
            obj.save();
        });
        $(elem + " .button_close").click(function () {
            layerCloseAll();
            obj.close();
        });
        $(elem + " .button_ok").focus();
    };

    /**
     * 新增界面表单生成 自定义样式
     * @param obj
     */
    var addFormInit2 = function (obj) {
        var elem = obj.elem;
        var inputs = obj.inputs;
        var laydateIds = [];
        var select2Ids = [];

        function getTextInput(i, input) {
            let requiredHtml = "";
            if (input.required !== undefined) {
                requiredHtml = '<span style="color:red;">*</span>';
            }
            var type = input.type === undefined ? "text" : input.type;
            if (type === "hidden") {
                return getInputHtml(formId, input);
            }
            if (input.placeholder === undefined) {
                input.placeholder = "请输入" + input.text;
            }
            return '<tr>' +
                '        <td class="label">' + input.text + ':</td>' +
                '        <td class="input">' + getInputHtml(formId, input) + '</td>' +
                '        <td class="info">' + requiredHtml + '</td>' +
                '   </tr>';
        }

        function getTextareaInput(i, input) {
            let requiredHtml = "";
            if (input.required !== undefined) {
                requiredHtml = '<span style="color:red;">*</span>';
            }
            return '<tr>' +
                '        <td class="label">' + input.text + ':</td>' +
                '        <td class="input"><textarea type="text" id="' + formId + '_' + input.name + '" name="' + input.name + '" placeholder="请输入' + input.text + '" ' + getInputParam(input) + ' ></textarea></td>' +
                '        <td class="info">' + requiredHtml + '</td>' +
                '   </tr>';
        }

        function getSelectInput(i, item) {
            let requiredHtml = "";
            if (item.required !== undefined) {
                requiredHtml = '<span style="color:red;">*</span>';
            }
            var optionHtml = "";
            if (item.options !== undefined)
                $.each(item.options, function (i, item) {
                    optionHtml += '<option value=' + item.value + '>' + item.text + '</option>';
                });
            return '<tr>' +
                '     <td class="label">' + item.text + '：</td>' +
                '     <td class="input">' +
                '           <select name="' + item.name + '" class="form-control" ' + getInputParam(item) + '>' +
                '               <option value="">请选择</option>' +
                '               ' + optionHtml +
                '           </select>' +
                '     </td>' +
                '     <td class="info">' + requiredHtml + '</td>' +
                '   </tr>';
        }

        function getHtml(i, item) {
            var html = "";
            if (isTimeType(item)) {
                laydateIds.push({id: formId + '_' + item.name, type: item.type});
                item.type = "text";
                html += getTextInput(i, item);
            } else if (item.type === "zTree") {
                const data = {
                    id: formId + '_' + item.name,
                    param: item.param
                };
                html += getTextInput(i, item);
                select2Ids.push(data);
            } else if (item.type === "textarea") {
                html += getTextareaInput(i, item);
            } else {
                html += getTextInput(i, item);
            }
            return html;
        }

        var html;
        var html_left = "";
        var html_right = "";
        var formId = $(elem).attr('id') + "Form";
        $.each(inputs, function (i, item) {
            html_left += getHtml(i, item);
        });
        html = '<table class="panelForm">' + html_left + '</table>';
        var formHtml =
            '<form id="' + formId + '">' +
            '    <div class="split"></div>' +
            '       <div class="content">' +
            '           ' + html + '' +
            '       </div>' +
            '       <div class="split"></div>' +
            '    <div class="btnBar"><input value="保存" class="okBtn zhuanjiao_btn" type="button"/><input value="取消" class="quxiao_btn" type="button"/></div>' +
            '</form>';
        $.ajaxSettings.async = false;
        $(elem).empty();
        $(elem).append(formHtml);
        initLayDate(laydateIds);
        setSelectData(elem, inputs);
        $.ajaxSettings.async = true;
        $(elem + " .zhuanjiao_btn").click(function () {
            obj.save();
        });
        $(elem + " .quxiao_btn").click(function () {
            obj.close();
        });
    };

    /**
     * 横向布局
     * @param obj
     */
    function addFormInitH(obj) {
        var elem = obj.elem;

        var inputs = obj.inputs;
        var laydateIds = [];
        var select2Ids = [];

        function getTextInput(i, input) {
            let requiredHtml = "";
            if (obj.isHideMoreInfoBtn) {
                requiredHtml = 'verificationInput';
            } else {
                if (input.required !== undefined) {
                    requiredHtml = 'verificationInput';
                }
            }

            var type = input.type === undefined ? "text" : input.type;
            if (type === "hidden") {
                return getInputHtml(formId, input);
            }
            if (input.placeholder === undefined) {
                input.placeholder = "请输入" + input.text;
            }
            return '<div class="block ' + requiredHtml + '">' +
                '        <div class="label">' + input.text + '：</div>' +
                '        <div class="input1 input">' +
                '          ' + getInputHtml(formId, input) +
                '        </div>' +
                '   </div>';
        }

        function getTextareaInput(i, input) {
            let requiredHtml = "";
            if (input.required !== undefined) {
                requiredHtml = '<span style="color:red;">*</span>';
            }
            return '<div>' +
                '        <div class="label">' + input.text + '：</div>' +
                '        <div class="input"><textarea type="text" id="' + formId + '_' + input.name + '" name="' + input.name + '" placeholder="请输入' + input.text + '" ' + getInputParam(input) + ' ></textarea></div>' +
                '   </div>';
        }

        function getHtml(i, item) {
            var html = "";
            if (isTimeType(item)) {
                laydateIds.push({id: formId + '_' + item.name, type: item.type});
                item.type = "text";
                html += getTextInput(i, item);
            } else if (item.type === "zTree") {
                const data = {
                    id: formId + '_' + item.name,
                    param: item.param
                };
                html += getTextInput(i, item);
                select2Ids.push(data);
            } else if (item.type === "textarea") {
                html += getTextareaInput(i, item);
            } else {
                html += getTextInput(i, item);
            }
            return html;
        }

        var html;
        var html_left = "";
        var formId = $(elem).attr('id') + "Form";
        $.each(inputs, function (i, item) {
            html_left += getHtml(i, item);
        });
        html = '<table class="panelForm">' + html_left + '</table>';
        var formHtml =
            '<div class="split"></div>' +
            '<div class="content" style="overflow:auto">' +
            '     <div class="formContent">' +
            '          <form id="' + formId + '">' +
            '                ' + html + '' +
            '          </form>';
        if (!obj.isHideMoreInfoBtn) {
            formHtml += ' <div class="slideInput_box">' +
                '             <input type="button" class="slideDownBtn" onclick="slideDownInput(this)" value="更多信息">' +
                '             <input type="button" class="slideUpBtn" onclick="slideUpInput(this)" value="收回信息" style="display: none;">' +
                '         </div>';
        }

        formHtml += '     </div>' +
            '</div>';
        $.ajaxSettings.async = false;
        $(elem).empty();
        $(elem).append(formHtml);
        initLayDate(laydateIds);
        setSelectData(elem, inputs, true);
        $.ajaxSettings.async = true;
    }

    /**
     * 绑定时间
     * @param laydateIds
     */
    function initLayDate(laydateIds, call) {
        if (laydateIds.length > 0) {
            $.each(laydateIds, function (i, item) {
                laydate.render({
                    elem: '#' + item.id,
                    type: item.type,
                    isInitValue: true,
                    done: call
                });
            })
        }
    }

    function setSelectData(elem, inputs, autoWidthType) {
        $.each(inputs, function (i, item) {
            if (item.type === "select") {
                if (item.param !== undefined) {
                    var forName = item.param.for;
                    if (forName !== undefined) {
                        //级联触发事件
                        if($(elem + " [name='" + forName + "']").val() !="" || $(elem + " [name='" + forName + "']").val() !=undefined){
                            requestSelectData(1, elem, item, undefined, autoWidthType);
                             }
                        $(elem + " [name='" + forName + "']").change(function (ev, changeCall) {
                            requestSelectData(1, elem, item, changeCall, autoWidthType);
                        });
                    } else {
                        requestSelectData(1, elem, item, undefined, autoWidthType);
                    }
                } else if (item.options !== undefined) {
                    requestSelectData(2, elem, item, undefined, autoWidthType);
                }
                $(elem + " [name='" + item.name + "']").attr("readonly", "readonly").change(function () {
                    var val = $(this).val();
                    var id = $(this).attr("id");
                    $("#" + id + "_Opt").wdfSel("setVal", val);
                });
            } else if (item.type === "zTree") {
                initSelectZtree(elem, item, autoWidthType);
                $(elem + " [name='" + item.name + "']").attr("readonly", "readonly").change(function () {
                    var val = $(this).val();
                    var id = $(this).attr("id");
                    $("#" + id + "_Opt").wdfSel("setVal", val);
                });
            }
        });
    }

    function requestSelectData(type, elem, inputsItem, changeCall, autoWidthType) {
        if (type === 1) {
            var param = inputsItem.param;
            var url = param.url;
            var forId = param.for;
            if (forId !== undefined) {
                var parentId = $(elem + " [name='" + forId + "']").val();

                url += parentId;
            }
            $.get(url, function (obj) {
                var optData = [];
                var data = eval("obj." + param.data);
                if (data !== undefined)
                    $.each(data, function (i, item) {
                        if (!item[param.value] || !item[param.text])
                            return;
                        var value = eval("item." + param.value);
                        var text = eval("item." + param.text);
                        optData.push({id: value, name: text, data: item})
                    });
                initSelect(elem, inputsItem, optData, param, autoWidthType);
                if (changeCall !== undefined)
                    changeCall.call(this, optData)
            });
        } else if (type === 2) {
            var optData = [];
            $.each(inputsItem.options, function (i, item) {
                optData.push({id: item.value, name: item.text, data: item})
            });
            param = {value: "value", text: "text"};
            initSelect(elem, inputsItem, optData, param, autoWidthType);
        }
    }

    function initSelect(elem, inputsItem, data, param, autoWidthType) {
        var opt = selectOpt(elem, inputsItem, data, param, autoWidthType);

        if(param.isSelectFirst){
            opt.event.onZTreeLoaded = function (zTree, selObj,zTreeNodes) {
                if(zTreeNodes.length>0){
                    $(selObj).wdfSel("setVal",zTreeNodes[0].id);
                }

            }
        }
        $("#" + opt.id).wdfSel("init", opt);
    }

    function initSelectZtree(elem, item, autoWidthType) {

        var elemObj = $(elem + " [name='" + item.name + "']");
        var param = item.param;
        var url = param.url;
        var optData = [];
        $.get(url, function (obj) {//同步
            var data = eval("obj." + param.data);
            if (data !== undefined)
                $.each(data, function (i, item) {
                    var id = eval("item." + param.id);
                    var pId = eval("item." + param.pId);
                    var name = eval("item." + param.name);
                    var realID = eval("item." + param.realID);
                    optData.push({id: id, pId: pId, name: name, realID: realID})
                });
        });
        let opt = selectZtreeOpt(elemObj, optData, param, autoWidthType);
        $("#" + opt.id).wdfSel("init", opt);

    }

    function selectZtreeOpt(elemObj, data, param, autoWidthType) {

        var elemId = elemObj.attr('id');//addDivFrom_orgId
        var elemName = elemObj.attr('name');
        var required = elemObj.attr('required');
        required = required === undefined ? "" : required;
        var placeholderName = elemObj.attr("placeholder").replace("输入", "选择");
        elemObj.attr("type", "hidden");
        elemObj.nextAll().remove();//删除同胞, 避免TM 的这个每次初始化多会生成一个新的

        var required = elemObj.attr('required');
        required = required === undefined ? "" : required;

        var disabled = elemObj.attr('disabled');
        elemObj.removeAttr("disabled");
        disabled = disabled === undefined ? "" : "disabled='disabled'";

        var newElemId = elemId + "_Opt";
        var newElemName = elemName + "_Opt";
        elemObj.parent().append('<input id="' + newElemId + '" name="' + newElemName + '" placeholder="' + placeholderName + '" ' + required + '  ' + disabled + '>');

        var panelInputId = newElemId + "_hide";
        var panelInputName = elemName + "_hide";
        return {
            id: newElemId,          //渲染对象ID
            className: "wdfSel",          //样式名称
            zTreeOption: "default",       //zTree配置参数
            mulitValsChecked: false,       //是否为多值选择模式
            autoWidthType: autoWidthType,//true 文字适应 false 输入框适应
            height: 400,                  //下拉面板高度
            zIndex: 9999,                 //下面板层层级
            root: "data",                 //请求服务端返回的json对象的数据字段名称
            searchBar: {                  //搜索栏定义
                isOpen: true,                 //是否开启
                title: ""                     //标题
            },
            data: data,                     //要绑定到控件的数据，如果有该参数，则不会触发向服务端请求数据
            dataModel: {                  //后端对象到下拉树节点对象字段映射
                id: param.id,//树状id字段
                pId: param.pId,//父节点id字段
                name: param.name,//显示字段
                realID: param.realID,//真实id
                nodeType: "nodeType"//节点类型
            },
            valInput: {                   //控件值域的名称和ID用于表单的值提交
                id: panelInputId,
                name: panelInputName
            },
            isFixedHeight: true,          //是否固定下拉面板高度
            treeDataUrl: "", //非异步模式的树控件数据获取地址
            param: {//要提交的参数

            },
            dataFilter: function (nodes) {  //如果非异步模式的树控件数据过滤操作

            },
            event: {
                onZTreeLoaded: function (zTree, selObj) {
                },
                onChange: function (inputObj, selObj) {
                    var oldVal = $(elemObj).val();
                    var newVal = $("#" + panelInputId).val();
                    if (oldVal !== newVal && newVal !== "") {
                        $(elemObj).val(newVal).change();
                    }
                },
                onNodeClick: function (nodeObj) {
                    return true;
                }
            }
        };
    }

    function selectOpt(elem, inputsItem, data, param, autoWidthType) {

        var elemObj = $(elem + " [name='" + inputsItem.name + "']");
        var elemId = elemObj.attr('id');//addDivFrom_orgId
        var elemName = elemObj.attr('name');
        var required = elemObj.attr('required');
        required = required === undefined ? "" : required;
        var disabled = elemObj.attr('disabled');
        elemObj.removeAttr("disabled");
        disabled = disabled === undefined ? "" : "disabled='disabled'";
        var placeholderName="";
        if(elemObj.attr("placeholder")){
            placeholderName = elemObj.attr("placeholder").replace("输入", "选择");
        }

        elemObj.val("");
        elemObj.data("obj", data);
        elemObj.attr("type", "hidden");
        elemObj.nextAll().remove();//删除同胞, 避免TM 的这个每次初始化多会生成一个新的

        var newElemId = elemId + "_Opt";
        var newElemName = elemName + "_Opt";
        elemObj.parent().append('<input id="' + newElemId + '" name="' + newElemName + '" placeholder="' + placeholderName + '" ' + required + ' ' + disabled + '>');

        var panelInputId = newElemId + "_hide";
        var panelInputName = elemName + "_hide";
        //编辑机构类型
        return {
            id: newElemId,          //渲染对象ID
            className: "wdfSel",          //样式名称
            zTreeOption: "default",       //zTree配置参数
            mulitValsChecked: inputsItem.mulitValsChecked?true:false,       //是否为多值选择模式
            autoWidthType: autoWidthType,//true 文字适应 false 输入框适应
            height: 366,                  //下拉面板高度
            zIndex: 9999,                 //下面板层层级
            root: "data",                 //请求服务端返回的json对象的数据字段名称
            searchBar: {                  //搜索栏定义
                isOpen: true,                 //是否开启
                title: ""                     //标题
            },
            data: data,                     //要绑定到控件的数据，如果有该参数，则不会触发向服务端请求数据
            valInput: {                   //控件值域的名称和ID用于表单的值提交
                id: panelInputId,
                name: panelInputName
            },
            isFixedHeight: true,          //是否固定下拉面板高度
            treeDataUrl: "", //非异步模式的树控件数据获取地址
            param: {},
            dataFilter: function (nodes) {  //如果非异步模式的树控件数据过滤操作
            },
            event: {
                onZTreeLoaded: function (zTree, selObj) {
                },
                onChange: function (inputObj, selObj) {
                    var oldVal = $(elemObj).val();
                    var newVal = $("#" + panelInputId).val();
                    if (oldVal !== newVal && newVal !== "") {
                        $(elemObj).val(newVal).change();
                        var key = param.value;

                        $.each(data, function (i, item) {
                            var obj = item.data;
                            if (newVal == obj[key]) {
                                $(elemObj).trigger("select", [obj]);
                                return false;
                            }
                        });
                    }
                },
                onNodeClick: function (nodeObj) {
                    return true;
                }
            }

        };
    }

    function isTimeType(item) {
        if (item.type === "datetime" || item.type === "time" || item.type === "date" || item.type === "month" || item.type === "year") {
            if (item.type === "time") {
                item.type = "date"
            }
            return true
        }
        return false;
    }

    function getInputHtml(formId, obj, name) {
        if (name === undefined)
            name = obj.name;
        if (obj.placeholder === undefined) {
            obj.placeholder = obj.text;
        }
        return '<input type=' + obj.type + ' id="' + formId + '_' + name + '" name="' + name + '" ' + getInputParam(obj) + '/>'
    }

    function getInputParam(obj) {
        var inputParam = "";
        for (var key in obj) {
            var value = obj[key];
            if (value === undefined)
                continue;
            if (key === "name" || key === "id" || key === "options" || key === "params")
                continue;
            switch (key) {
                case "required":
                    if (value) inputParam += ' required ';
                    break;
                case "readonly":
                    if (value) inputParam += ' readonly="readonly" ';
                    break;
                case "disabled":
                    if (value) inputParam += ' disabled="disabled" ';
                    break;
                default:
                    inputParam += key + '="' + value + '" ';
            }
        }
        return inputParam;
    }

    $.fn.extend({
        select: function (value) {
            var isFunction = $.isFunction(value);
            if (!isFunction) {
                $(this).val(value).change();
            }
        }
    });

    return {
        screenInit: screenInit,//查询表单
        addFormInit: addFormInit,//bootstrap
        addFormInit2: addFormInit2,//系统自带样式
        addFormInitH: addFormInitH//横向布局
    }
}
();

